<?php
namespace jy_mlianfee;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class member extends \plugin{
	/*
		核实分销商,$isset无条件成为分销商
	*/
	public function checkmember($mid=0,$isexi=false,$isset=false){
			$mid=$mid?:$_SESSION['uid'];
			$member=$this->get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			$getinfo=model('core/member')->detail($mid,array('nickname','avatar','realname','mobile','wechat'));
			$member['realname']=$member['realname']?:$getinfo['realname'];
			$member['mobile']=$member['mobile']?:$getinfo['mobile'];
			$member['wechat']=$member['wechat']?:$getinfo['wechat'];
			$page_setting=$this->plugin_setting('page_setting');
			$page_setting['tagent']=$page_setting['tagent']?:'代理商';
			if(!$member['mid']){
				$this->register(array('mid'=>$mid));
				$member=$this->get('member',array('mid'=>$mid));
			}
			if(!$member['is_agent']){
				//如果成为分销商无条件，此处自动申请成为分销商
				if($this->setting['become']==3 || $isset){
					//分销商无需审核，此处自动审核
					$ispass=true;
					$this->apply_agent($member['mid']);
					if($this->setting['become_check'] && $isexi){
						exi('申请正在审核请耐心等待！','error','jy_mlianfee/apply/');
					}elseif($isset){
						return true;
					}
				}elseif($this->setting['become']==2){
					if($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_mlianfee/apply/');
					}
				}elseif($this->setting['become']==4){
					$isbuyfee=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$mid),'price');
					if($isbuyfee>=$this->setting['become_orderprice'] || !$this->setting['become_orderprice']){
						$ispass=true;
						$this->apply_agent($member['mid']);
					}
					if($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_mlianfee/apply/');
					}
				}
				if($ispass && $isexi){
					exi('','','jy_mlianfee/index/');
				}
			}elseif($member['stop'] && $isexi){
				if($isset){
						return true;
				}
				exi('你的'.$page_setting['tagent'].'身份已禁用！','error','jy_mlianfee/apply/');
			}elseif((!$member['realname'] || !$member['mobile']) && $isexi){
				if($isset){
					return true;
				}
				if($this->setting['isinfo']==1){
					exi('请先完善信息','error','jy_mlianfee/info/');
				}else{
					
				}
			}
			return $member;
	}
	/*
		会员注册
	*/
	public function register($member=array()){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('生态会员注册失败，缺少会员ID！');
		}
		$ismid=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid']),array('mid'));
		if($ismid){
			return false;
		}
		//file_put_contents(ROOT_D.'/1.txt','become'.$this->setting['become']);
		//如果成为分销商无条件，此处自动申请成为分销商
		if($this->setting['become']==3){
			//分销商无需审核，此处自动审核
			$this->apply_agent($member['mid']);
		}elseif($this->setting['become']==4){
			$isbuyfee=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$member['mid']),'price');
			if($isbuyfee>=$this->setting['become_orderprice'] || !$this->setting['become_orderprice']){
				$this->apply_agent($member['mid']);
			}
		}
	}
	public function member_update($mid,$update=array()){
		pdo_update('core_members',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$mid));
		return true;
	}
	/*
		申请成为生态
	*/
	public function apply_agent($mid,$area_code='',$post=array()){
		//file_put_contents(ROOT_D.'/1.txt','apply_agent');
		$new_apply=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		$new_apply['fromuser']=pdo_getcolumn('core_members',array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']),'fromuser');
		$this->insert('member',$new_apply);
		$id=$this->insertid();
		if(!$this->setting['become_check']){
			//分销商无需审核，此处自动审核
			$this->check_agent($mid,1);
		}
		return $id;
	}
	/*
		审核生态
	*/
	public function check_agent($fromuser,$status=0,$remark=''){
		if(!$fromuser || !$status){
			return;
		}
		$member=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$fromuser));
		if(!$member){
			return;
		}
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			//审核通过，如果会员还不是代理商，此处修改
			$this->update('member',array('is_agent'=>1),array('mid'=>$member['mid'],'status'=>0));
		}else{
			$update['remark']=$remark;
		}
		$this->update('member',$update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$fromuser));
		if($update['status']==1){
			$this->model('notice')->notice_fenxiao_add($member['mid']);
		}
		return true;
	}
	public function set_parent($mid,$fromuser){
		$this->update('member',array('fromuser'=>$fromuser),array('mid'=>$mid));
		$this->model('notice')->notice_fenxiao_xiajiadd($mid,$fromuser);
		return true;
	}
	/*
		禁用代理商
	*/
	public function stop_agent($fromuser){
		$this->update('member',array('stop'=>1),array('mid'=>$fromuser));
	}
	/*
		恢复代理商
	*/
	public function start_agent($fromuser){
		$this->update('member',array('stop'=>0),array('mid'=>$fromuser));
	}
	//直接开通等级
	public function up_arealevel($mid,$level_id=0){
		$member=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		if($member['id']){
			if($level_id>$member['level_id']){
				$this->update('member',array('level_id'=>$level_id),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
			}
		}else{
			$new_apply=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'status'=>1,
				'level_id'=>$level_id,
				'createtime'=>TIMESTAMP,
			);
			$this->insert('member',$new_apply);
			$id=$this->insertid();
		}	
		return $id;
	}
	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($mid,$orderid=0){
		//file_put_contents(ROOT_D.'/1.txt',"update_agent_level\n",FILE_APPEND);
		$member=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		if(!$member['id']){
			$this->register();
			$member=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		}
		if(!$member['is_agent'] || $member['stop']){
			return;
		}
		$level=$this->get('level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['level_id']));
		$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'level >'=>$level['level']),
			'orderby'=>'level desc',
		);
		$levels=$this->getall('level',$parameter['params'],'','',$parameter['orderby']);
		if(empty($levels)){
			return;
		}
		$downmids=$this->getteamids($mid);
		$level_up_types=$this->model()->current_level_up_types();//获取当前设置的加盟商等级升级条件
		foreach($levels as $lv){
			$conditions=json_decode($lv['conditions'],true);
			if(!$level_up_types || !$lv['id']){
				break;
			}
			$up=true;
			foreach($level_up_types as $type){
				//file_put_contents(ROOT_D.'/1.txt',$type."~\n",FILE_APPEND);
				if(!$this->level_up_types[$type]){
					continue;
				}
				//一次性自购金额
                if($type=='one_buyprice'){
					$myorder=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$mid),'price','price desc');
					if($myorder['price']<$conditions['one_buyprice'] && $conditions['one_buyprice']>0){
						$up=false;
						//file_put_contents(ROOT_D.'/1.txt',$conditions['one_buyprice'].'~~'.$myorder['price']."~one_buyprice\n",FILE_APPEND);
						 break;
					}
                }elseif($type=='self_order_num'){
                    //自购订单总数
					$isbuynum=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$mid),'id');
					if($isbuynum<$conditions['self_order_num'] && $conditions['self_order_num']>0){
						$up=false;
						 break;
					}
                }elseif($type=='self_order_price'){
					$isbuyfee=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$mid),'price');
					if($isbuyfee<$conditions['self_order_price'] && $conditions['self_order_price']>0){
						$up=false;
						 break;
					}
				}
			}
			if($up){
				$next_level=$lv;
				break;
			}
		}
		if($up){
			$update=array(
				'level_id'=>$next_level['id'],
			);
			$this->update('member',$update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		}
		return true;
	}
	public function getteamids($teammid,$nomy=false){
		$newmembers=array();
		$newteammids=$teammid;
		for($i=1;$i<9;$i++){
		    $params=array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$newteammids);
			$members=pdo_getall('core_members',$params,array('id'),'id');
			if($members){
				foreach($members as $key=>$item){
					$newmembers[$key]=$key;
				}
				$newteammids=array_keys($members);
			}else{
				break;
			}
		}
		if($newmembers){
			$teamids=array_keys($newmembers);
		}else{
			$teamids=$nomy?array():array($teammid);
		}
		return $teamids;
	}
	public function check_level_type($level_type,$nomy=false){
        $mid=$_SESSION['uid'];
		$levelset=pdo_getcolumn('jy_mlianfee_level',array('uniacid'=>$_SESSION['uniacid'],'level'=>$level_type),'conditions');
		$conditions=$levelset?json_decode($levelset,true):array('no'=>1);
		//一次性自购金额
        if($conditions['one_buyprice']){
					$myorder=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$mid),'price','price desc');
					if($myorder['price']<$conditions['one_buyprice'] && $conditions['one_buyprice']>0){
						exi('一次性自购金额最大金额不足￥'.$conditions['one_buyprice'],'error');
					}
         }
		 if($conditions['self_order_num']){
                    //自购订单总数
					$isbuynum=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$mid),'id');
					if($isbuynum<$conditions['self_order_num'] && $conditions['self_order_num']>0){
						exi('自购订单总数不足'.$conditions['self_order_num'],'error');
					}
        }
		if($conditions['self_order_price']){
					$isbuyfee=pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$mid),'price');
					if($isbuyfee<$conditions['self_order_price'] && $conditions['self_order_price']>0){
						exi('自购订单总金额不足￥'.$conditions['self_order_price'],'error');
					}
		}
		return true;
	}
	public function member_feelog($mid=0,$field='g_sffee',$value=0,$is_add=false,$type=0,$remark=''){
		//file_put_contents(ROOT_D.'/1.txt','~member_feelog~'.$field);
		$mid=$mid?:$_SESSION['uid'];
		if($is_add){
			$data=array($field.' +='=>$value);
		}else{
			$fieldfee=pdo_getcolumn('jy_mlianfee_member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),$field);
			if($fieldfee<$value){
				return false;
			}else{
				$data=array($field.' -='=>$value);
			}
		}
		pdo_update('jy_mlianfee_member',$data,array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		$log=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'type'=>$type,
				'fee'=>$value,
				'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_mlianfee_feelog',$log);
		$log_id=pdo_insertid();
		return $log_id?:$field;
	}
}